package org.apache.lucene.demo;

import java.io.IOException;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.harzone.commons.hzdb.lucene.impl.server.LuceneServer;

public class ThreadGroupSearch extends Thread{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private Query query;
	private Filter filter;
	private LuceneServer luceneServer;
	private GroupValue rootGroupValue;
	private String group1;
	private String group2;
	
	public ThreadGroupSearch(GroupValue rootGroupValue,Query query,Filter filter,String group1,String group2,LuceneServer luceneServer) {
		this.query=query;
		this.luceneServer=luceneServer;
		this.rootGroupValue=rootGroupValue;
		this.filter=filter;
		
		this.group1=group1;
		this.group2=group2;
	}
	
	@Override
	public void run() {
		//初始化group结果的存储对象
		GroupValue groupValue=new GroupValue();
		IndexSearcher searcher = luceneServer.acquire();
		GroupCollector groupCollector=new GroupCollector(groupValue,group1,group2);
		try {
			TopDocs topDocs=searcher.search(query,filter, 1);
			long start = System.currentTimeMillis();
			searcher.search(query,filter, groupCollector);
			long end = System.currentTimeMillis();
			logger.info("group count:"+topDocs.totalHits+" time:"+(end-start)+"ms");
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			luceneServer.release(searcher);
		}
		rootGroupValue.putRootmap(groupValue.getBayonetMap());
	}
}
